//
//  HJTabbarController.m
//  HJShop
//
//  Created by apple on 2019/4/15.
//  Copyright © 2019 HJShop. All rights reserved.
//

#import "HJTabbarController.h"
#import "TYBaseNavigationController.h"
#import "HJWeexViewController.h"

@interface HJTabbarController () <UITabBarControllerDelegate>

@property (nonatomic, strong) NSMutableArray *tabBarItems;

@end

@implementation HJTabbarController

#pragma mark - LazyLoad
- (NSMutableArray *)tabBarItems {
    
    if (_tabBarItems == nil) {
        _tabBarItems = [NSMutableArray array];
    }
    
    return _tabBarItems;
}

#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    [self addDcChildViewContorller];
    
    self.selectedViewController = [self.viewControllers objectAtIndex:HJTabBarControllerHome];
}

#pragma mark - 添加子控制器
- (void)addDcChildViewContorller
{
    NSArray *childArray = @[
                            @{MallClassKey  : @"HJHomeViewController",
                              MallTitleKey  : @"美信",
                              MallImgKey    : @"tabr_01_up",
                              MallSelImgKey : @"tabr_01_down",
                              MallWeexKey   : @""},
                            
                            @{MallClassKey  : @"HJHomeViewController",
                              MallTitleKey  : @"首页",
                              MallImgKey    : @"tabr_02_up",
                              MallSelImgKey : @"tabr_02_down",
                              MallWeexKey   : @""},
                            
                            @{MallClassKey  : @"HJWeexViewController",
                              MallTitleKey  : @"美媒榜",
                              MallImgKey    : @"tabr_03_up",
                              MallSelImgKey : @"tabr_03_down",
                              MallWeexKey   : @"tuijian"},
                            
                            @{MallClassKey  : @"HJWeexViewController",
                              MallTitleKey  : @"美店",
                              MallImgKey    : @"tabr_04_up",
                              MallSelImgKey : @"tabr_04_down",
                              MallWeexKey   : @"detail"},
                            
                            @{MallClassKey  : @"HJMineBaseViewController",
                              MallTitleKey  : @"我的",
                              MallImgKey    : @"tabr_05_up",
                              MallSelImgKey : @"tabr_05_down",
                              MallWeexKey   : @""},
                            
                            ];
    [childArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = [NSClassFromString(dict[MallClassKey]) new];
        if ([vc isKindOfClass:[HJWeexViewController class]]) {
            [(HJWeexViewController *)vc setJsPath:dict[MallWeexKey]];
        }
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        UITabBarItem *item = nav.tabBarItem;
        item.image = [UIImage imageNamed:dict[MallImgKey]];
        item.selectedImage = [[UIImage imageNamed:dict[MallSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.imageInsets = UIEdgeInsetsMake(6, 0,-6, 0);//（当只有图片的时候）需要自动调整
        [self addChildViewController:nav];
        if ([dict[MallTitleKey] isEqualToString:@"美信"]) {
            //self.beautyMsgVc = (DCBeautyMessageViewController *)vc; //给美信赋值
        }
        
        // 添加tabBarItem至数组
        [self.tabBarItems addObject:vc.tabBarItem];
    }];
}

#pragma mark - 控制器跳转拦截
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    HJWeexViewController *weexController = nil;
    if ([viewController isKindOfClass:[HJWeexViewController class]]) {
        weexController = (HJWeexViewController *)viewController;
    }

    if(viewController == [tabBarController.viewControllers objectAtIndex:HJTabBarControllerMediaList]){
        [weexController setJsPath:@"tuijian"];
        
        return YES;
    }
    return YES;
}

#pragma mark - 点击动画
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据自己需求改动
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
    
}

#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //点击tabBarItem动画
    [self tabBarButtonClick:[self getTabBarButton]];
    if ([self.childViewControllers.firstObject isEqual:viewController]) { //根据tabBar的内存地址找到美信发通知jump
        [[NSNotificationCenter defaultCenter] postNotificationName:@"jump" object:nil];
    }
    
}

- (UIControl *)getTabBarButton{
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
